package org.pupil.config.provider;


import org.pupil.annotation.RpcService;
import org.pupil.common.Cache;
import org.pupil.common.URL;
import org.pupil.common.constants.Register;
import org.pupil.config.Properties;
import org.pupil.config.RpcProperties;
import org.pupil.filter.FilterFactory;
import org.pupil.invoke.InvokerFactory;
import org.pupil.register.RegistryFactory;
import org.pupil.register.RegistryService;
import org.pupil.socket.compress.CompressFactory;
import org.pupil.socket.serialization.SerializationFactory;
import org.pupil.socket.server.Server;
import org.pupil.utils.IpUtil;
import org.pupil.utils.ServiceNameBuilder;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.BeanPostProcessor;



/**
 * @Description: 服务提供方后置处理器
 * @Author: pupil
 * @Date: 2024/07/21 下午 1:38
 */

public class ProviderPostProcessor implements BeanPostProcessor, InitializingBean {

    RpcProperties rpcProperties;

    private final String ip = IpUtil.getIP();

    public ProviderPostProcessor(RpcProperties rpcProperties) {
        this.rpcProperties = rpcProperties;
    }


    @Override
    public void afterPropertiesSet() throws Exception {
        RegistryFactory.init();
        FilterFactory.initServer();
        InvokerFactory.init();
        SerializationFactory.init();
        CompressFactory.init();
        Thread t = new Thread(() -> {
            Server server = new Server(rpcProperties.getPort());
            try {
                server.run();
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        t.setDaemon(true); // 开启线程守护
        t.start();
    }

    // 注册bean
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        Class<?> beanClass = bean.getClass();
        // 找到bean上带有 RpcService 注解的类
        RpcService rpcService = beanClass.getAnnotation(RpcService.class);
        if (rpcService != null) {
            // 可能会有多个接口,默认选择第一个接口
            String serviceName = beanClass.getInterfaces()[0].getName();
            if (!rpcService.serviceInterface().equals(void.class)) {
                serviceName = rpcService.serviceInterface().getName();
            }
            try {
                RegistryService registryService = RegistryFactory.get(rpcProperties.getRegistry().getName());
                final URL url = new URL();
                url.setPort(Properties.getPort());
                url.setIP(ip);
                url.setVersion(rpcService.version());
                url.setServiceName(serviceName);
                // 服务注册
                registryService.register(url);
                // 缓存
                String key = ServiceNameBuilder.buildServiceKey(serviceName, rpcService.version());
                Cache.SERVICE_MAP.put(key, bean);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        return bean;
    }
}
